package com.imy.touch.root.touch;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.imy.common.root.RootTools;
import com.imy.common.shell.execution.Command;
import com.imy.common.shell.execution.Shell;
import com.imy.common.util.ALog;
import com.imy.touch.root.file.FilePushHelper;

import java.io.IOException;

public class ServerStartHelper {
    private String exitPackages = "";
    private Shell shell;
    private String startServerResult = "";
    private Handler mHandler = new android.os.Handler(Looper.getMainLooper());

    public void start(Context context, final ServerStartListener listener) {
        try {
            shell = RootTools.getShell(true);
            boolean b = FilePushHelper.pushTouchFile(context);
            if (b) {
                checkInstall(new InstallCallBack() {
                    @Override
                    public void isInstall(boolean b) {
                        if (!b) {
                            installRootTouch(new InstallRootTouchCallback() {
                                @Override
                                public void onSuccess() {
                                    //start server
                                    startServer(listener);
                                }

                                @Override
                                public void onFail() {
                                    listener.onFail();
                                }
                            });
                        } else {
                            startServer(listener);
                        }
                    }
                });
            }
        } catch (Exception e) {
            listener.onFail();
        }
    }

    private ServerStartListener listener;

    private void startServer(final ServerStartListener listener) {
        this.listener = listener;

        Command command = new Command(1002, "am instrument -w -r    -e debug false -e class 'com.imy.rootserver.TouchServer#useAppContext' com.imy.rootserver.test/androidx.test.runner.AndroidJUnitRunner") {
            @Override
            public void commandCompleted(int id, int exitcode) {
                super.commandCompleted(id, exitcode);
            }

            @Override
            public void commandOutput(int id, String line) {
                super.commandOutput(id, line);
                ALog.ti("line:"+line);
                if (id==1002&& line.equals("INSTRUMENTATION_STATUS_CODE: 1")) {
                    mHandler.removeCallbacks(timeOutHandler);
                    listener.onSuccess();
                }

            }
        };
        try {
            mHandler.postDelayed(timeOutHandler, 2000);
            shell.add(command);
        } catch (IOException e) {
            mHandler.removeCallbacks(timeOutHandler);
            listener.onFail();
            e.printStackTrace();
        }
    }

    private Runnable timeOutHandler = new Runnable() {
        @Override
        public void run() {
                if (listener!=null){
                    listener.onFail();
                }
        }
    };

    private void installRootTouch(final InstallRootTouchCallback callback) {
        try {
            final Command command = new Command(1001, "pm install " + FilePushHelper.ANDROID_TMP_PATH) {
                @Override
                public void commandOutput(int id, String line) {
                    super.commandOutput(id, line);
                    if (id == 1001 && line.equals("Success")) {
                        callback.onSuccess();
                    } else {
                        callback.onFail();
                    }
                    ALog.ti("installRootTouch commandOutput:" + line);
                }

                @Override
                public void commandCompleted(int id, int exitcode) {
                    super.commandCompleted(id, exitcode);
                    ALog.ti("installRootTouch commandCompleted:" + exitcode);

                }
            };
            shell.add(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void checkInstall(final InstallCallBack callBack) {
        //判断文件是否已经安装
        try {
            final Command command = new Command(1000, "pm list packages com.imy.rootserver") {
                @Override
                public void commandCompleted(int id, int exitcode) {
                    super.commandCompleted(id, exitcode);
                    ALog.ti("checkInstall commandCompleted:" + exitcode);
                    if (id == 1000) {
                        if (exitPackages.equals("")) {
                            //未安装
                            callBack.isInstall(false);
                        } else {
                            callBack.isInstall(true);
                        }
                    }
                }

                @Override
                public void commandOutput(int id, String line) {
                    super.commandOutput(id, line);
                    if (id == 1000) {
                        ALog.ti( " checkInstall commandOutput:" + line);
                        exitPackages = line;
                    }
                }
            };

            shell.add(command);
        } catch (Exception e) {
            callBack.isInstall(false);
        }

    }
}
